package cn.kongwg.java;

import cn.kongwg.util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2021-01-29 9:12
 */
@WebServlet("/cookieServlet")
public class CookieServlet extends BaseServlet {

    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("key2", "cookie2");
        resp.addCookie(cookie);
        resp.getWriter().write("cookie创建成功");
        Cookie cookie1 = new Cookie("key3", "cookie3");
        resp.addCookie(cookie);
        resp.getWriter().write("cookie创建成功");

    }

    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();

        for (Cookie cookie : cookies) {
            resp.getWriter().write("cookie的名字为: " + cookie.getName() + "值为：" + cookie.getValue());
        }

        Cookie key1 = CookieUtil.findCookie("key1", cookies);

        if (key1 != null) {
            resp.getWriter().write("找到想要的cookie值" + key1.getName());
        }

    }

    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       /* Cookie cookie = new Cookie("key1", "newValue");
        resp.addCookie(cookie);
        resp.getWriter().write("cookie1已修改");

        */

        //方案二
        Cookie key1 = CookieUtil.findCookie("key1", req.getCookies());

        key1.setValue("newnewValue");
        resp.addCookie(key1);
        resp.getWriter().write("修改完成");
    }

    protected void lifeCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defalutlife", "defalutlife");
        cookie.setMaxAge(-1);
        resp.addCookie(cookie);
        resp.getWriter().write(cookie.getMaxAge());
    }

    protected void life1Cookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defalutlife1", "defalutlife1");
        cookie.setMaxAge(0);
        resp.addCookie(cookie);
        resp.getWriter().write(cookie.getMaxAge());
    }

    protected void life2Cookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defalutlife2", "defalutlife2");
        cookie.setMaxAge(60 * 60);
        resp.addCookie(cookie);
        resp.getWriter().write(cookie.getMaxAge());
    }

    protected void pathCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("path", "path");

        cookie.setPath(req.getContextPath()+"/abc");

        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带路径的cookie");
    }
   protected void loginCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       String username = req.getParameter("username");
       String password = req.getParameter("password");

       if("root".equals(username) && "root".equals(password)){
           Cookie cookie = new Cookie("username", username);
           resp.addCookie(cookie);
           resp.getWriter().write("登录成功");
       }else{
           resp.getWriter().write("登录失败");
       }

   }


}
